package ctrl

import (
	"im_unitree/service"
	utils "im_unitree/utils"
	"net/http"
)

var userService service.UserService

func UserLogin(writer http.ResponseWriter, request *http.Request) {
	// 解析参数
	request.ParseForm()
	// 获取账号密码
	mobile := request.PostForm.Get("mobile")
	passwd := request.PostForm.Get("passwd")

	user, err := userService.Login(mobile, passwd)
	if err != nil {
		utils.RespFail(writer, err.Error())
	} else {
		utils.RespOk(writer, user, "")
	}
}

func UserRegister(writer http.ResponseWriter, request *http.Request) {
	request.ParseForm()
	mobile := request.PostForm.Get("mobile")
	plainpwd := request.PostForm.Get("passwd")
	avatar := request.PostForm.Get("avatar")
	sex := request.PostForm.Get("sex")
	nickname := request.PostForm.Get("nickname")
	memo := request.PostForm.Get("memo")

	user, err := userService.Register(mobile, plainpwd, nickname, avatar, sex, memo)
	if err != nil {
		utils.RespFail(writer, err.Error())
	} else {
		utils.RespOk(writer, user, "")
	}

}
